package development.app;

import java.util.Vector;

import state.Clip;
import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.SMSHandler;
import state.SendSMSListener;
import state.component.GameScrollBar;
import state.font.FontManager;
import state.font.ImageText;
import state.socket.DataPackage;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.array.StringArray;
import development.Constants;
import development.DrawUtils;
import development.Global;
import development.Utils;
import development.bean.SMSServiceCommand;
import development.bean.Subscription;
import development.group.FrmGroupThoiTietTinTuc;
import development.socket.SocketClientUtil;

public class FrmThoiTietTinhThanh extends GameForm implements SendSMSListener {
  public static final int GET_THOITIET_LIST_REQUEST = 1100;
  public static final int GET_THOITIET_LIST_RESPONSE = 1100;

  private GameImage iconlistimage;
  private GameImage[] iconStatusImages;
  private ImageText text8;
  private ImageText textMenu;
  private int chooseIndex;
  public int AreaIndex;
  private final int DX = 0;
  private final int DY = 25;
  int distanceRow = 40;
  int dx = 0;
  int dy = 25;
  int currentpage;
  int maxpage;
  int iStatus;
  String[] listareacode;
  String[] listareaname;
  String title;
  Vector vtlist;
  boolean isshowdetail = false;
  int type;// 0 search from main 1 next from main
  private int numberOfItem;

  private FrmThoiTiet frmThoiTiet;

  private static GameForm singleton;
  private GameScrollBar scrollBar;
  private Clip textView; // Là khung nhìn để vẽ đoạn text ra

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmThoiTietTinhThanh();
    }
    return singleton;
  }

  public void init(Hashtable hashtable) {
    iconStatusImages = new GameImage[4];
    iconlistimage = ImageUtil.getImage(Constants.MENU_IMAGE_CATEGORY, "5.Thoi-tiet.png");
    iconStatusImages[0] = ImageUtil.getImage(Constants.THOI_TIET_CATEGORY, "icon_nang_big.png");
    iconStatusImages[1] = ImageUtil.getImage(Constants.THOI_TIET_CATEGORY, "icon_mua_big.png");
    iconStatusImages[2] = ImageUtil.getImage(Constants.THOI_TIET_CATEGORY, "icon_nangmay_big.png");
    iconStatusImages[3] = ImageUtil.getImage(Constants.THOI_TIET_CATEGORY, "icon_may_big.png");

    frmThoiTiet = (FrmThoiTiet) FrmThoiTiet.instance();
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);

    textMenu = FontManager.getFont(FontManager.FONT_SIZE_13);

    currentpage = 0;
    listareacode = frmThoiTiet.AREA_CODE[AreaIndex];
    listareaname = frmThoiTiet.AREANAME[AreaIndex];
    numberOfItem = frmThoiTiet.AREA_CODE[AreaIndex].length;
    distanceRow = 46;
    dx = DX;
    dy = DY;
    chooseIndex = 0;
    vtlist = new Vector();

    textView = new Clip(5, dy, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT - 50); // Là khung nhìn để vẽ đoạn text ra
    scrollBar = new GameScrollBar(Constants.SCREEN_WIDTH - 5, 25, 5, Constants.SCREEN_HEIGHT - 50, textView, _manager);
    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    DrawUtils.drawAppBackground(g);

    if (!isshowdetail) {
      int y = 0;
      for (int row = 0; row < numberOfItem; row++) {
        y = dy + distanceRow * (row);
        if (row == chooseIndex) {
          g.setColor(0x4EA2D0);
          g.fillRect(0, y, GameConstants.SCREEN_WIDTH, distanceRow);
        }
        g.drawImage(iconlistimage, dx + 18, y + 20, GameConstants.CENTER_ANCHOR);
        textMenu.drawParagraph2(g, listareaname[row], GameColor.YELLOW_CODE, 170, dx + 45, y + 12, scrollBar);
        g.setColor(0xC0C0C0);
        g.drawLine(0, y + distanceRow, GameConstants.SCREEN_WIDTH, y + distanceRow);
      }
    } else {
      int HOZ_DISTANCE = dy;
      if (!vtlist.isEmpty()) {
        for (int row = 0; row < numberOfItem; row++) {
          iStatus = getStatusImage((String) vtlist.elementAt(row));
          g.drawImage(iconStatusImages[iStatus], 10, HOZ_DISTANCE + 10, GameConstants.TOP_LEFT_ANCHOR);
          HOZ_DISTANCE = text8.drawParagraph2(g, (String) vtlist.elementAt(row), GameColor.YELLOW_CODE, 155, 75, HOZ_DISTANCE + 10, scrollBar);
          if (HOZ_DISTANCE < iconStatusImages[iStatus].getHeight())
            HOZ_DISTANCE = iconStatusImages[iStatus].getHeight();
          g.setColor(0xC0C0C0);
          g.drawLine(0, HOZ_DISTANCE + 5, GameConstants.SCREEN_WIDTH, HOZ_DISTANCE + 5);
        }
      }
    }
    scrollBar.draw(g);

    if (!isshowdetail) {
      DrawUtils.setBottomBar(_manager, "", "Chọn", "Trở về");
      text8.drawString2(g, frmThoiTiet.AREA_NAME[AreaIndex], GameColor.WHITE_CODE, 10, 5, GameConstants.TOP_LEFT_ANCHOR);
    } else {
      text8.drawString2(g, title, GameColor.WHITE_CODE, 10, 5, GameConstants.TOP_LEFT_ANCHOR);
      DrawUtils.setBottomBar(_manager, "", "", "Trở về");
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.RIGHT:
      FrmGroupThoiTietTinTuc.setTab(1);
      break;
    case Key.DOWN:
      if (!isshowdetail) {
        chooseIndex = (chooseIndex + 1 > numberOfItem - 1) ? chooseIndex : chooseIndex + 1;
        int down = dy + distanceRow * chooseIndex;
        if (down > GameConstants.SCREEN_HEIGHT - (50 + DrawUtils.getBottomBarHeight() + 25))
          dy -= distanceRow;
      } else {
        if (dy > -GameConstants.SCREEN_HEIGHT)
          dy -= distanceRow;
      }
      break;
    case Key.UP:
      if (!isshowdetail) {
        chooseIndex = (chooseIndex - 1 < 0) ? chooseIndex : chooseIndex - 1;
        int up = dy + distanceRow * chooseIndex;
        if (up < 25)
          dy += distanceRow;
      } else {
        if (dy < 25)
          dy += distanceRow;
      }

      break;
    case Key.FIRE:
      if (isshowdetail)
        return;
      Subscription sub = Utils.getSubcription("TT", listareacode[chooseIndex]);
      if (sub == null || sub.status == Subscription.EXPIRED) {
        SMSServiceCommand sms = Utils.getSMSServiceCommand("TT", listareacode[chooseIndex]);
        if (sms != null) {
          Global.alert.showAlert1(this, GameAlert.YES_NO_TYPE, StringArray.create1( "Phí dịch vụ 10 gold", "Bạn đồng ý không?" ), 10000);
          return;
        }
      }
      DataPackage dataPackage = DataPackage.createDataPackage1(GET_THOITIET_LIST_REQUEST);
      dataPackage.putString(listareacode[chooseIndex]);
      Global.appClient.write(dataPackage);
      title = listareaname[chooseIndex];
      vtlist = new Vector();
      numberOfItem = 0;
      dy = DY;
      Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(5000);
      break;
    case Key.SOFT_RIGHT:
      if (type == 0) {
        FrmGroupThoiTietTinTuc frmGroupThoiTietTinTuc = (FrmGroupThoiTietTinTuc) FrmGroupThoiTietTinTuc.instance();
        frmGroupThoiTietTinTuc.getMultiTab().setTab(0, frmThoiTiet, null);
        chooseIndex = 0;
      }
      if (!isshowdetail) {
        FrmGroupThoiTietTinTuc frmGroupThoiTietTinTuc = (FrmGroupThoiTietTinTuc) FrmGroupThoiTietTinTuc.instance();
        frmGroupThoiTietTinTuc.getMultiTab().setTab(0, frmThoiTiet, null);
        chooseIndex = 0;
      } else {
        isshowdetail = false;
        numberOfItem = frmThoiTiet.AREA_CODE[AreaIndex].length;
        dy = DY;
        chooseIndex = 0;
      }
      break;
    }
  }

  public void onRecieveData(DataPackage dataPackage) {
    int header = dataPackage.getHeader();
    GameGlobal.systemCanvas.hideAlert();
    switch (header) {
    case GET_THOITIET_LIST_RESPONSE:
      isshowdetail = true;
      String s = dataPackage.nextString();
      vtlist.addElement(s);
      numberOfItem = vtlist.size();
      break;
    }
  }

  private int getStatusImage(String data) {
    data = data.toLowerCase();
    if (data.indexOf("it may, troi nang") >= 0)
      return 2;
    else if (data.indexOf("co mua") >= 0)
      return 1;
    else if (data.indexOf("troi nang") >= 0)
      return 0;
    else if (data.indexOf("nhieu may, khong mua") >= 0)
      return 3;
    else
      return 0;
  }

  public void destroy() {
    frmThoiTiet = null;
    iconlistimage = null;
    iconStatusImages = null;
    text8 = null;
    textMenu = null;
    listareacode = null;
    listareaname = null;
    title = null;
    vtlist = null;
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
    if (alertId == 10000 && eventType == GameAlert.YES_BUTTON) {
      SMSServiceCommand sms = Utils.getSMSServiceCommand("TT", listareacode[chooseIndex]);
      if (sms != null) {
        SMSHandler.getInstance(this).sendMsg(sms.serviceNumber, sms.command);
      }
    }
  }

  public void onSendSMSFail() {
    Global.alert.showAlert(this, GameAlert.OK_TYPE, "Đăng ký thất bại");
  }

  public void onSendSMSSuccess() {
    Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create2("Đăng ký thành công","Xin mời bạn quay lại","trong ít phút"));
    SocketClientUtil.createSubscription("TT", listareacode[chooseIndex]);
  }
}
